const { mysql } = require('../qcloud')

module.exports = async ctx => {
  const { bookid , openid} = ctx.request.query
  let mysqlSelect = mysql('comment')
    .select('comment.*', 'cSessionInfo.user_info')
    .join('cSessionInfo', 'comment.openid', 'cSessionInfo.open_id')
    // .where('bookid', bookid)
    let res
    if(bookid){
      res = await mysqlSelect.where('bookid', bookid)
    }else if(openid){
      res = await mysqlSelect.where('openid',openid)
    }
  ctx.state.data = {
    msg: res.map(v=>{
      let info = JSON.parse(v.user_info)
      return Object.assign({},v,{
        nickname: info.nickName,
        image: info.avatarUrl
      })
    })
  }
}